;(function() {
	'use strict';
	angular.module('services.omdisplay.operationDevelop')
		.service('odService', OdService)
		.factory('dnDevelopNum', dnDevelopNum)
		.factory('dcDevComplete', dcDevComplete)
		.factory('dpDevProportion', dpDevProportion)
		.factory('dcdDevChnlDuty', dcdDevChnlDuty)
		.factory('dpDeveloperProblems', dpDeveloperProblems);

	function OdService(dnDevelopNum, dcDevComplete, dpDevProportion, dcdDevChnlDuty, dpDeveloperProblems) {
			this.dnDevelopNum = dnDevelopNum;
			this.dcDevComplete = dcDevComplete;
			this.dpDevProportion = dpDevProportion;
			this.dcdDevChnlDuty = dcdDevChnlDuty;
			this.dpDeveloperProblems = dpDeveloperProblems;
			this.getEchartsData = function(res, title, echartsLegendName, valueName, seriesName) {
				var temp = {
					echartsLegend: [],
					echartsSeries: [],
				};
				angular.forEach(res.data, function(item, index) {
					if (temp.echartsLegend.indexOf(item[echartsLegendName]) === -1) {
						temp.echartsLegend.push(item[echartsLegendName]);
					}
					temp.echartsSeries[index] = {
						name: item[echartsLegendName],
						value: item[valueName]
					};
				});
				var option = {
					title: {
						text: title,
						x: 'center'
					},
					animation: false,
					tooltip: {
						show: true
					},
					legend: {
						show: true,
						orient: 'vertical',
						x: 'right',
						y: 'center',
						data: temp.echartsLegend
					},
					series: [{
						name: seriesName,
						type: 'pie',
						radius: '55%',
						center: ['50%', '60%'],
						itemStyle: {
							normal: {
								label: {
									show: false
								},
								labelLine: {
									show: false
								}
							}
						},
						data: temp.echartsSeries
					}]
				};
				return option;
			};
		}
		//问题数
	function dnDevelopNum(Restangular) {
			return {
				//运维开发数
				queryDevelopNum: function(params, callback) {
					Restangular.all('sas/OMdisplay/devWork').post(params).then(callback);
				},
				//各业务问题数对比
				queryBusinessContrast: function(param, callback) {
					Restangular.all('sas/OMdisplay/businessContrast').post(param).then(callback);
				},
				getPeopleData: function() {
					return [{
						peopleName: '张三',
						peopleCode: '1'
					}, {
						peopleName: '李四',
						peopleCode: '2'
					}, {
						peopleName: '王五',
						peopleCode: '3'
					}];
				},
				getEchartsData: function(items, title) {
					var seriesObj = {};
					var seriesData = {};
					var temp = {
						echartsXData: [],
						echartsLegend: [],
						echartsSeries: [],
						echartsTitleText: title
					};
					angular.forEach(items, function(item) {
						if (temp.echartsXData.indexOf(item.channelName) === -1) {
							temp.echartsXData.push(item.channelName);
						}
						if (temp.echartsLegend.indexOf(item.developerName) === -1) {
							temp.echartsLegend.push(item.developerName);
						}
						seriesObj[item.developerName] = [];
						seriesData[item.developerName] = {};
					});
					angular.forEach(temp.echartsXData, function(x) {
						angular.forEach(items, function(item) {
							seriesData[item.developerName][x] = 0;
						});
					});
					angular.forEach(items, function(item) {
						seriesData[item.developerName][item.channelName] = item.total;
					});
					angular.forEach(seriesData, function(s, index) {
						angular.forEach(temp.echartsXData, function(x) {
							seriesObj[index].push(s[x]);
						});
					});
					angular.forEach(temp.echartsLegend, function(el, i) {
						temp.echartsSeries[i] = {
							name: el,
							type: 'bar',
							barMaxWidth: 120,
							data: seriesObj[el]
						};
					});
					var echartsOption = this.echartsExamples(temp);
					return echartsOption;
				},
				echartsExamples: function(temp) {
					var option = {
						title: {
							text: temp.echartsTitleText,
							x: 'center'
						},
						animation: false,
						// dataZoom: {
						// 	show: true,
						// 	realtime: true
						// },
						grid: {
							x2: 100
						},
						tooltip: {
							show: true
						},
						legend: {
							show: true,
							orient: 'vertical',
							data: temp.echartsLegend,
							x: 'right',
							y: 'center'
						},
						xAxis: [{
							type: 'category',
							data: temp.echartsXData
						}],
						yAxis: [{
							type: 'value',
							boundaryGap: [0, 0.01]
						}],
						series: temp.echartsSeries
					};
					return option;
				},
				getPageBind: function() {
					var year = new Date().getFullYear();
					var month = new Date().getMonth();
					return {
						startTime: new Date(year,month),
						endTime: new Date(year,month),
						system: '11',
						business: [],
						channel: [],
						people: [],
					};
				},
				getTitle: function(send) {
					if (send.startTime === send.endTime) {
						return send.startTime + '开发完成' + send.systemName.toString() + send.businessName.toString() + '业务' + send.channelName + '渠道' + '开发数';
					} else {
						return send.startTime + '_' + send.endTime + '开发完成' + send.systemName.toString() + send.businessName.toString() + '业务' + send.channelName + '渠道' + '开发数';
					}
				}
			};
		}
		//运维开发完成情况
	function dcDevComplete(Restangular) {
			return {
				//运维开发完成情况
				queryDevComplete: function(params, callback) {
					Restangular.all('sas/OMdisplay/devComplete').post(params).then(callback);
				},
				getDevCompleteTitle: function(send, modelName) {
					var title = {};
					send.systemName.length === 0 ? title.systemName = '' : title.systemName = send.systemName.toString();
					if (send.startTime === send.endTime) {
						return title.systemName + send.startTime + send.businessName.toString() + '业务' + modelName;
					} else {
						return title.systemName + send.startTime + '_' + send.endTime + send.businessName.toString() + '业务' + modelName;
					}
				},
				getDevCompleteEchartsPie: function(res, send) {
					var title = this.getDevCompleteTitle(send, '开发人员完成情况');
					var option = {
						title: {
							text: title,
							x: 'center'
						},
						animation: false,
						tooltip: {
							show: true
						},
						legend: {
							show: true,
							orient: 'vertical',
							x: 'right',
							y: 'center',
							data: ['已完成', '未完成', '解决中']
						},
						series: [{
							name: '开发完成情况',
							type: 'pie',
							radius: '55%',
							center: ['50%', '60%'],
							itemStyle: {
								normal: {
									label: {
										show: false
									},
									labelLine: {
										show: false
									}
								}
							},
							data: [{
								value: res.data[0].completed,
								name: '已完成'
							}, {
								value: res.data[0].solving,
								name: '未完成'
							}, {
								value: res.data[0].unfinished,
								name: '解决中'
							}]
						}]
					};
					return option;
				}
			};
		}
		//运维开发完成占比
	function dpDevProportion(Restangular) {
			return {
				queryDevProportion: function(params, callback) {
					Restangular.all('sas/OMdisplay/devProportion').post(params).then(callback);
				}
			};
		}
		//渠道开发分配
	function dcdDevChnlDuty(Restangular) {
			return {
				queryDevChnlDuty: function(params, callback) {
					Restangular.all('sas/OMdisplay/devChnlDuty').post(params).then(callback);
				}
			};
		}
		//开发人员问题数对比
	function dpDeveloperProblems(Restangular) {
		return {
			queryNum: function(params, callback) {
				Restangular.all('sas/OMdisplay/developerContrast').post(params).then(callback);
			}
		};
	}
})();